home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell⁄THINK C / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  5.6 KB  |  233 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     CShell
  5. ** File:        window.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  19. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __SYSEQU__
  27. #include <SysEqu.h>
  28. #endif
  29.  
  30. #ifndef __UTILITIES__
  31. #include "Utilities.h"
  32. #endif
  33.  
  34.  
  35.  
  36. /*****************************************************************************/
  37.  
  38.  
  39.  
  40. extern short    gPrintPage;        /* Non-zero means we are printing. */
  41.  
  42.  
  43.  
  44. /*****************************************************************************/
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. /* This function creates a new application window.  An application window
  50. ** contains a document which is referenced by a handle in the refCon field.
  51. */
  52.  
  53. #pragma segment Window
  54. OSErr    AppNewWindow(FileRecHndl frHndl, WindowPtr *retWindow)
  55. {
  56.     WindowPtr        oldPort, window;
  57.     OSErr            err;
  58.  
  59.     /* We will allocate our own window storage instead of letting the Window
  60.     ** Manager do it because GetNewWindow may load in temp. resources before
  61.     ** making the NewPtr call, and this can lead to heap fragmentation.
  62.     */
  63.  
  64.     GetPort(&oldPort);
  65.  
  66.     err = memFullErr;        /* Assume that we will fail.  Good attitude. */
  67.  
  68.     if (window = GetStaggeredWindow(rWindow, nil, FrontWindow(), (WindowPtr)-1, true)) {
  69.         (*frHndl)->fileState.window = window;
  70.         SetWRefCon(window, (long)frHndl);
  71.         AppNewWindowTitle(window);
  72.         if (!(err = AppNewWindowControls(frHndl, window))) {
  73.             if (gPrintPage)
  74.                 MoveWindow(window, 16384, 16384, true);
  75.                     /* So the window can be hidden while printing, yet
  76.                     ** PrintMonitor can get the document name. */
  77.             ShowWindow(window);
  78.             if (gPrintPage)
  79.                 MoveWindow(window, 16384, 16384, true);
  80.                     /* Moving invisible windows to the front doesn't always
  81.                     ** get them to the front.  Now that it is visible, moving
  82.                     ** it will definitely get it to the front. */
  83.         }
  84.         else DisposeAnyWindow(window);
  85.     }
  86.  
  87.     SetPort(oldPort);
  88.     if (retWindow) *retWindow = window;
  89.  
  90.     return(err);
  91. }
  92.  
  93.  
  94.  
  95. /*****************************************************************************/
  96.  
  97.  
  98.  
  99. /* This function updates the window title to reflect the new document name.
  100. ** The new document name is stored in the fileState portion of the document.
  101. ** This is automatically set to 'Untitled # N' for new documents, and is
  102. ** updated when a user does a save-as.
  103. */
  104.  
  105. #pragma segment Window
  106. void    AppNewWindowTitle(WindowPtr window)
  107. {
  108.     FileRecHndl    frHndl;
  109.     Str255        wTitle;
  110.  
  111.     if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  112.         pstrcpy((char *) &wTitle, (char *) &(*frHndl)->fileState.fss.name);
  113.         SetWTitle(window, wTitle);
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. /*****************************************************************************/
  120.  
  121.  
  122.  
  123. /* This function returns the state of the window's document.  If the document
  124. ** is dirty, then true is returned.  If the document is clean, or the window
  125. ** has no document, then false is returned.
  126. */
  127.  
  128. #pragma segment Window
  129. Boolean    AppWindowDirty(WindowPtr window)
  130. {
  131.     FileRecHndl    frHndl;
  132.  
  133.     if (frHndl = (FileRecHndl)GetWRefCon(window))
  134.         return(AppDocumentDirty(frHndl));
  135.  
  136.     return(false);
  137. }
  138.  
  139.  
  140.  
  141. /*****************************************************************************/
  142.  
  143.  
  144.  
  145. /* Close all the windows.  This is called prior to quitting the application.
  146. ** This function returns true if all windows were closed.  The user may decide
  147. ** to abort a save, thus stopping the closing of the windows.  If the user
  148. ** does this, false will be returned, indicating that all windows were not
  149. ** closed after all.
  150. */
  151.  
  152. #pragma segment Window
  153. Boolean    DisposeAllWindows(void)
  154. {
  155.     WindowPtr    window;
  156.     
  157.     while (window = *(WindowPtr *)WindowList) {
  158.  
  159.         /* While we have a front window, try closing it. */
  160.  
  161.         if (!DisposeOneWindow(window, iQuit)) return(false);
  162.             /* When DisposeOneWindow returns false, this means that the window
  163.             ** didn't close.  The only cause of this is if the window had a
  164.             ** document that needed saving, and the user cancelled the save.
  165.             ** If the window succeeded in getting closed, then we are
  166.             ** returned true.  Either way, we return the result.
  167.             */
  168.     }
  169.  
  170.     return(true);
  171. }
  172.  
  173.  
  174.  
  175. /*****************************************************************************/
  176.  
  177.  
  178.  
  179. /* Closes one window.  This window may be an application window, or it may be
  180. ** a system window.  If it is an application window, it may have a document
  181. ** that needs saving.
  182. */
  183.  
  184. #pragma segment Window
  185. Boolean    DisposeOneWindow(WindowPtr window, short saveMode)
  186. {
  187.     FileRecHndl    frHndl;
  188.     OSErr        err;
  189.  
  190.     if (IsAppWindow(window)) {
  191.         /* First, if the window is an application window, try saving
  192.         ** the document.  Remember that the user may cancel the save.
  193.         */
  194.  
  195.         if (frHndl = (FileRecHndl)GetWRefCon(window)) {
  196.  
  197.             err = AppSaveDocument(frHndl, window, saveMode);
  198.             if (err) {
  199.                 if (err != userCanceledErr)
  200.                     Alert(rErrorAlert, nil);
  201.                 return(false);
  202.             }        /* Stop closing windows on error or user cancel. */
  203.  
  204.             SendMessage(frHndl, kDisconnectMssg);
  205.             AppDisposeDocument(frHndl);
  206.                 /* The document is saved, or the user doesn't care about
  207.                 ** that document, so dispose of the document.
  208.                 */
  209.         }
  210.     }
  211.     DisposeAnyWindow(window);
  212.     return(true);
  213. }
  214.  
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219.  
  220.  
  221. #pragma segment Window
  222. WindowPtr    SetFilePort(FileRecHndl frHndl)
  223. {
  224.     WindowPtr    oldPort;
  225.  
  226.     GetPort(&oldPort);
  227.     SetPort((*frHndl)->fileState.window);
  228.     return(oldPort);
  229. }
  230.  
  231.  
  232.  
  233.